package org.quotidian.jinger_project;

/* GetPost - Decompiled by JODE
 * Visit http://jode.sourceforge.net/
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletResponse;

public class GetPost {
	private java.lang.String NEWLINE = "\n";
	private int errorCode = 0;

	public GetPost() {
		NEWLINE = java.lang.System.getProperty("line.separator");
	}

	
	
	// REF - Complete
	// TODO - Rework the handling of properties to be more standard
	private URLConnection getConnection(String url, Hashtable<String, String> requestProperties) throws IOException {
		//System.out.println("GetPost.getConnection()");
		URLConnection urlconnection;
		URL urlObject = new URL(url);
		urlconnection = urlObject.openConnection();
		urlconnection.setDoInput(true);
		urlconnection.setDoOutput(true);
		urlconnection.setUseCaches(false);
		if (requestProperties != null) {
			Enumeration<String> enumeration = requestProperties.keys();
			while (enumeration.hasMoreElements()) {
				String temp = enumeration.nextElement();
				urlconnection.setRequestProperty(temp, requestProperties.get(temp));
			}
		}
		if (urlconnection == null) {
			throw new IOException("Could not create the URLConnection for an unknown reason.");
		}
		return urlconnection;
	}

	//REF - Complete
	private void setErrorCode(URLConnection urlConnection) throws IOException {
		//System.out.println("GetPost.setErrorCode()");
		if (HttpURLConnection.class.isAssignableFrom(urlConnection.getClass())) {
			HttpURLConnection httpurlconnection = (HttpURLConnection) urlConnection;
			setErrorCode(httpurlconnection.getResponseCode());
			System.out.println(httpurlconnection.getResponseCode());
		}
	}
	
	
	
	// REF - In Progress
	public String doGet(String url, Hashtable<String, String> requestProperties, String forceEncoding, HttpServletResponse response) {
		
		//return doGetSSL(url, requestProperties, forceEncoding, response);
		
		
		//System.out.println("GetPost.doGet()");
		// Create connection
		URLConnection urlConnection = null;
		try {
			urlConnection = getConnection(url, requestProperties);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			setErrorCode(urlConnection);
			if (getErrorCode() == response.SC_FOUND) {
				String loc = urlConnection.getHeaderField("Location");
				if (loc != null) {
					response.sendRedirect(loc);
					return null;
				} else {
					//response.sendError(response.SC_INTERNAL_SERVER_ERROR, "Failed to redirect a 302");
				}
				/* Prints the header fields */
				/*
				Map<String, List<String>> flds = urlConnection.getHeaderFields();
				for(String fld : flds.keySet()) {
					for(String str : flds.get(fld)) {
						System.out.println(fld + " : " + str);
					}
				}
				*/
			}
			
			
			
			String contentType = urlConnection.getContentType();
			if (contentType != null && forceEncoding != null) {
				contentType = rewriteEncoding(contentType, forceEncoding);
			}
			String encoding = urlConnection.getContentEncoding();
			if (encoding == null) {
				encoding = "";
			}
			
			
			if (contentType != null) {
				response.setContentType(contentType);
			} else {
				contentType = "";
			}

			
			InputStream local;
			BufferedOutputStream bufferedoutputstream;
			try {
				if (encoding.indexOf("gzip") >= 0) {
					local = new GZIPInputStream(urlConnection.getInputStream());
				} else {
					local = new BufferedInputStream(urlConnection.getInputStream());
				}
				
				
				
				bufferedoutputstream = new BufferedOutputStream(response.getOutputStream());

				if (contentType.contains("text/html")) {
					BufferedInputStream blocal = new BufferedInputStream(local);
					BufferedReader breader = new BufferedReader(new InputStreamReader(blocal));
					StringBuffer sb = new StringBuffer();
					String line;
					while ((line = breader.readLine()) != null) {
						//if (line.contains("<head>")) {
						//	sb.append(line.substring(0, line.indexOf("<head>") + 6));
						//	sb.append("<script src='http://kordikaj2.fs.udayton.edu:8080/jinger/slip.js'></script>");
						//	sb.append(line.substring(line.indexOf("<head>") + 6));
						//} else {
							sb.append(line);
						//}
					}
					bufferedoutputstream.write(sb.toString().getBytes());
				} else {
					int i;
					while ((i = (local).read()) >= 0) {
						bufferedoutputstream.write(i);
					}
				}

			} catch (Exception exception) {
				return getMessage(url, exception);
			}
			if (local != null) {
				(local).close();
			}
			if (bufferedoutputstream != null) {
				bufferedoutputstream.flush();
				bufferedoutputstream.close();
			}
		} catch (java.lang.Exception exception) {
			return getMessage(url, exception);
		}
		return null;
		
	}

	
	
	
	
	
	
	
	
	
	
	
	public void setErrorCode(int i) {
		errorCode = i;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public String doAction(java.lang.String string, java.lang.String string_0_, java.util.Hashtable hashtable, java.util.Hashtable hashtable_1_, int i, java.lang.String string_2_, java.lang.String string_3_, java.lang.String encoding, javax.servlet.ServletInputStream servletinputstream, javax.servlet.http.HttpServletResponse httpservletresponse) {
		//System.out.println("UNREF - GetPost.doAction()");
		if (i > 0) {
			java.util.Properties properties = java.lang.System.getProperties();
			properties.put("sun.net.client.defaultConnectTimeout", "" + i);
			properties.put("sun.net.client.defaultReadTimeout", "" + i);
			java.lang.System.setProperties(properties);
		}
		if (string_2_ != null && string_3_ != null) {
			java.util.Properties properties = java.lang.System.getProperties();
			properties.put("proxySet", "true");
			properties.put("proxyHost", string_2_);
			properties.put("proxyPort", string_3_);
			java.lang.System.setProperties(properties);
		}
		if ("GET".equals(string.toUpperCase())) {
			if (string_0_.toUpperCase().startsWith("HTTPS://"))
				return doGetSSL(string_0_, hashtable_1_, encoding, httpservletresponse);
			return doGet(string_0_, hashtable_1_, encoding, httpservletresponse);
		}
		if (string_0_.toUpperCase().startsWith("HTTPS://"))
			return doPostSSL(string_0_, hashtable, hashtable_1_, encoding, servletinputstream, httpservletresponse);
		return doPost(string_0_, hashtable, hashtable_1_, encoding, servletinputstream, httpservletresponse);
	}

	private java.lang.String doPost(java.lang.String string, java.util.Hashtable hashtable, java.util.Hashtable hashtable_5_, java.lang.String string_6_, javax.servlet.ServletInputStream servletinputstream, javax.servlet.http.HttpServletResponse httpservletresponse) {
		System.out.println("UNREF - GetPost.doPost()");
		java.lang.Object object = null;
		java.lang.Object object_7_ = null;
		java.lang.Object object_8_ = null;
		java.lang.Object object_9_ = null;
		java.lang.String string_10_ = "";
		if (hashtable != null)
			string_10_ = getParamsFromHash(hashtable);
		java.net.URLConnection urlconnection;
		try {
			java.net.URL url = new java.net.URL(string);
			urlconnection = url.openConnection();
			urlconnection.setDoOutput(true);
			urlconnection.setDoInput(true);
			urlconnection.setUseCaches(false);
			urlconnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
			if (hashtable_5_ != null) {
				java.util.Enumeration enumeration = hashtable_5_.keys();
				while (enumeration.hasMoreElements()) {
					java.lang.String string_11_ = (String) enumeration.nextElement();
					urlconnection.setRequestProperty(string_11_, (String) hashtable_5_.get(string_11_));
				}
			}
			java.io.PrintWriter printwriter = new java.io.PrintWriter(urlconnection.getOutputStream());
			printwriter.print(string_10_);
			int i;
			while ((i = servletinputstream.read()) != -1) {
				printwriter.write(i);
			}
			printwriter.close();
		} catch (java.lang.Exception exception) {
			return getMessage(string, exception);
		}
		try {
			if ((java.lang.Object) urlconnection instanceof java.net.HttpURLConnection) {
				java.net.HttpURLConnection httpurlconnection = (HttpURLConnection) urlconnection;
				setErrorCode(httpurlconnection.getResponseCode());
			}
			java.lang.String string_12_ = urlconnection.getContentType();
			if (string_12_ != null && string_6_ != null)
				string_12_ = rewriteEncoding(string_12_, string_6_);
			if (string_12_ != null) {
				httpservletresponse.setContentType(string_12_);
			}

			/*
			 * kordikaj testing
			 * httpservletresponse.setStatus(HttpServletResponse
			 * .SC_MOVED_TEMPORARILY); httpservletresponse.setHeader("Location",
			 * "http://localhost:8080/");
			 */

			java.lang.String string_13_ = urlconnection.getContentEncoding();
			if (string_13_ == null)
				string_13_ = "";
			InputStream local;
			java.io.BufferedOutputStream bufferedoutputstream;
			try {
				if (string_13_.indexOf("gzip") >= 0)
					(local) = (new java.util.zip.GZIPInputStream(urlconnection.getInputStream()));
				else
					(local) = (new java.io.BufferedInputStream(urlconnection.getInputStream()));
				bufferedoutputstream = new java.io.BufferedOutputStream(httpservletresponse.getOutputStream());
				int i;
				while ((i = (local).read()) >= 0)
					bufferedoutputstream.write(i);
			} catch (java.lang.Exception exception) {
				return getMessage(string, exception);
			}
			if (local != null)
				(local).close();
			if (bufferedoutputstream != null) {
				bufferedoutputstream.flush();
				bufferedoutputstream.close();
			}
		} catch (java.lang.Exception exception) {
			return getMessage(string, exception);
		}
		return null;
	}

	private java.lang.String doPostSSL(java.lang.String string, java.util.Hashtable hashtable, java.util.Hashtable hashtable_14_, java.lang.String string_15_, javax.servlet.ServletInputStream servletinputstream, javax.servlet.http.HttpServletResponse httpservletresponse) {
		System.out.println("UNREF - GetPost.doPostSSL()");
		java.lang.Object object = null;
		java.lang.Object object_16_ = null;
		java.lang.Object object_17_ = null;
		java.lang.Object object_18_ = null;
		java.lang.String string_19_ = "";
		if (hashtable != null)
			string_19_ = getParamsFromHash(hashtable);
		javax.net.ssl.HttpsURLConnection httpsurlconnection;
		try {
			java.net.URL url = new java.net.URL(string);
			httpsurlconnection = (HttpsURLConnection) url.openConnection();
			httpsurlconnection.setDoOutput(true);
			httpsurlconnection.setDoInput(true);
			httpsurlconnection.setUseCaches(false);
			httpsurlconnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
			if (hashtable_14_ != null) {
				java.util.Enumeration enumeration = hashtable_14_.keys();
				while (enumeration.hasMoreElements()) {
					java.lang.String string_20_ = (String) enumeration.nextElement();
					httpsurlconnection.setRequestProperty(string_20_, (String) hashtable_14_.get(string_20_));
				}
			}
			java.io.PrintWriter printwriter = new java.io.PrintWriter(httpsurlconnection.getOutputStream());
			printwriter.print(string_19_);
			int i;
			while ((i = servletinputstream.read()) != -1)
				printwriter.write(i);
			printwriter.close();
		} catch (java.lang.Exception exception) {
			return getMessage(string, exception);
		}
		try {
			if ((java.lang.Object) httpsurlconnection instanceof java.net.HttpURLConnection) {
				javax.net.ssl.HttpsURLConnection httpsurlconnection_21_ = httpsurlconnection;
				setErrorCode(httpsurlconnection_21_.getResponseCode());
			}
			java.lang.String string_22_ = httpsurlconnection.getContentType();
			if (string_22_ != null && string_15_ != null)
				string_22_ = rewriteEncoding(string_22_, string_15_);
			if (string_22_ != null)
				httpservletresponse.setContentType(string_22_);
			java.lang.String string_23_ = httpsurlconnection.getContentEncoding();
			if (string_23_ == null)
				string_23_ = "";
			InputStream local;
			java.io.BufferedOutputStream bufferedoutputstream;
			try {
				if (string_23_.indexOf("gzip") >= 0)
					(/* TYPE_ERROR */local) = (/* TYPE_ERROR */
					(new java.util.zip.GZIPInputStream(httpsurlconnection.getInputStream())));
				else
					(/* TYPE_ERROR */local) = (/* TYPE_ERROR */
					(new java.io.BufferedInputStream(httpsurlconnection.getInputStream())));
				bufferedoutputstream = new java.io.BufferedOutputStream(httpservletresponse.getOutputStream());
				int i;
				while ((i = (/* TYPE_ERROR */local).read()) >= 0)
					bufferedoutputstream.write(i);
			} catch (java.lang.Exception exception) {
				return getMessage(string, exception);
			}
			if (/* TYPE_ERROR */local != null)
				(/* TYPE_ERROR */local).close();
			if (bufferedoutputstream != null) {
				bufferedoutputstream.flush();
				bufferedoutputstream.close();
			}
		} catch (java.lang.Exception exception) {
			return getMessage(string, exception);
		}
		return null;
	}


	public String doGetSSL(String urlString, Hashtable<String,String> requestProperties, String forceEncoding, HttpServletResponse response) {
		System.out.println("UNREF - GetPost.doGetSSL()");
		java.lang.Object object = null;
		java.lang.Object object_32_ = null;
		java.lang.Object object_33_ = null;
		java.lang.Object object_34_ = null;

		HttpsURLConnection httpsurlconnection;
		try {
			URL url = new URL(urlString);
			httpsurlconnection = (HttpsURLConnection) url.openConnection();
			httpsurlconnection.setDoInput(true);
			httpsurlconnection.setUseCaches(false);
			if (requestProperties != null) {
				Enumeration enumeration = requestProperties.keys();
				while (enumeration.hasMoreElements()) {
					String temp = (String) enumeration.nextElement();
					httpsurlconnection.setRequestProperty(temp, (String) requestProperties.get(temp));
				}
			}
		} catch (java.lang.Exception exception) {
			return getMessage(urlString, exception);
		}

		try {
			if ((java.lang.Object) httpsurlconnection instanceof java.net.HttpURLConnection) {
				javax.net.ssl.HttpsURLConnection httpsurlconnection_36_ = httpsurlconnection;
				setErrorCode(httpsurlconnection_36_.getResponseCode());
			}
			java.lang.String string_37_ = httpsurlconnection.getContentType();
			if (string_37_ != null && forceEncoding != null)
				string_37_ = rewriteEncoding(string_37_, forceEncoding);
			if (string_37_ != null)
				response.setContentType(string_37_);
			else
				string_37_ = "";
			java.lang.String string_38_ = httpsurlconnection.getContentEncoding();
			if (string_38_ == null)
				string_38_ = "";
			InputStream local;
			java.io.BufferedOutputStream bufferedoutputstream;
			try {
				if (string_38_.indexOf("gzip") >= 0)
					(/* TYPE_ERROR */local) = (/* TYPE_ERROR */
					(new java.util.zip.GZIPInputStream(httpsurlconnection.getInputStream())));
				else
					(/* TYPE_ERROR */local) = (/* TYPE_ERROR */
					(new java.io.BufferedInputStream(httpsurlconnection.getInputStream())));
				bufferedoutputstream = new java.io.BufferedOutputStream(response.getOutputStream());
				int i;
				while ((i = (/* TYPE_ERROR */local).read()) >= 0)
					bufferedoutputstream.write(i);
			} catch (java.lang.Exception exception) {
				return getMessage(urlString, exception);
			}
			if (/* TYPE_ERROR */local != null)
				(/* TYPE_ERROR */local).close();
			if (bufferedoutputstream != null) {
				bufferedoutputstream.flush();
				bufferedoutputstream.close();
			}
		} catch (java.lang.Exception exception) {
			return getMessage(urlString, exception);
		}
		return null;
	}

	protected java.lang.String getMessage(java.lang.String string, java.lang.Exception exception) {
		System.out.println("UNREF - GetPost.getMessage()");
		java.lang.String string_39_ = exception.getClass().getName();
		int i = string_39_.lastIndexOf('.');
		string_39_ = string_39_.substring(i + 1);
		java.io.StringWriter stringwriter = new java.io.StringWriter();
		java.io.PrintWriter printwriter = new java.io.PrintWriter(stringwriter);
		exception.printStackTrace(printwriter);
		return ("Request: " + string + "\nException: " + string_39_ + ": " + exception.getMessage() + "\n" + stringwriter.getBuffer().toString());
	}

	private java.io.InputStreamReader getInputStreamReader(java.io.InputStream inputstream, java.lang.String string) throws java.io.UnsupportedEncodingException {
		System.out.println("UNREF - GetPost.getInputStreamReader()");
		if (string == null)
			return new java.io.InputStreamReader(inputstream);
		return new java.io.InputStreamReader(inputstream, string);
	}

	private java.lang.String rewriteEncoding(java.lang.String string, java.lang.String string_40_) {
		System.out.println("UNREF - GetPost.rewriteEncoding()");
		if (string.indexOf("charset") < 0)
			return string + ";charset=" + string_40_;
		int i = string.indexOf(";");
		if (i < 0)
			i = string.indexOf("charset");
		return string.substring(0, i) + ";charset=" + string_40_;
	}

	private java.lang.String getParamsFromHash(java.util.Hashtable hashtable) {
		System.out.println("UNREF - GetPost.getParamsFromHash()");
		java.lang.String string = "";
		java.lang.String string_41_ = "";
		java.util.Enumeration enumeration = hashtable.keys();
		while (enumeration.hasMoreElements()) {
			if (string.length() > 0)
				string += "&";
			string_41_ = (String) enumeration.nextElement();
			java.lang.String string_42_ = string_41_;
			int i = string_42_.indexOf("<");
			if (i > 0)
				string_42_ = string_42_.substring(0, i);
			string += string_42_ + "=";
			string += java.net.URLEncoder.encode((String) hashtable.get(string_41_));
		}
		return string;
	}
}
